package cqrtplm.controller;

import com.alibaba.fastjson.JSONObject;
import com.ty.basic.response.ResponseResult;
import cqrtplm.constant.RouteConstant;
import cqrtplm.entity.RtZdSqbDO;
import cqrtplm.service.RtZdService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(RouteConstant.ZD)
public class RtZdController {

    @Resource
    private RtZdService rtZdService;
    
    /**
     * 查询指定申请单（支持分页）
     */
    @PostMapping("/querySqb")
    public Map<String, Object> querySqb(@RequestBody Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = rtZdService.querySqb(params);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 查看指定申请单详情
     */
    @GetMapping("/getSqbDetail")
    public Map<String, Object> getSqbDetail(@Param("kid") String kid) {
        Map<String, Object> result = new HashMap<>();
        try {
            RtZdSqbDO sqb = rtZdService.getSqbDetail(kid);
            if (sqb != null) {
                result.put("success", true);
                result.put("data", sqb);
            } else {
                result.put("success", false);
                result.put("message", "未找到指定申请单");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 保存指定申请单（新增/修改）
     */
    @PostMapping("/saveSqb")
    public Map<String, Object> saveSqb(@RequestBody RtZdSqbDO sqb) {
        Map<String, Object> result = new HashMap<>();
        try {
            String kid = rtZdService.saveSqb(sqb);
            result.put("success", true);
            result.put("data", kid);
            result.put("message", "保存成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "保存失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除指定申请单
     */
    @GetMapping("/deleteSqb")
    public Map<String, Object> deleteSqb(@Param("kid") String kid) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = rtZdService.deleteSqb(kid);
            result.put("success", success);
            result.put("message", success ? "删除成功" : "删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 查询指定信息（支持分页）
     */
    @PostMapping("/queryZdInfo")
    public Map<String, Object> queryZdInfo(@RequestBody Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        try {
            result = rtZdService.queryZdInfo(params);
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    @PostMapping("/checkExist")
    public ResponseResult checkExist(@RequestBody JSONObject jsonObject) {
        boolean b = rtZdService.checkExist(jsonObject);
        return ResponseResult.success(b);
    }

    @PostMapping("/checkFormIdVersion")
    public ResponseResult checkFormIdVersion(@RequestBody JSONObject jsonObject){
        rtZdService.checkFormIdVersion(jsonObject);
        return ResponseResult.success();
    }
}